import { resourceEnv } from '@/config/test'

class Http {
    private fetch:any
    private defaultConfig:any
    constructor() {
        if (wx && wx.cloud) {
            wx.cloud.init()
            this.fetch = wx.cloud.callContainer
            this.defaultConfig = {
                config: {
                    env: resourceEnv
                },
                header: {
                    'X-WX-SERVICE': 'springboot-79y9', // xxx中填入服务名称（微信云托管 - 服务管理 - 服务列表 - 服务名称）
                }
            }
        } else {
            this.fetch = uni.request
        }
    }
    static getInstance () {
        return new Http()
    }
    async post (url:any, data?:any, showLoading:boolean=true, header?:any, isNotUserId?: boolean) {
        let params = this.getParams(url, data, header, isNotUserId)
        if (showLoading) {
            uni.showLoading()
        }
        try{
            let res = await this.fetch({
                ...params,
                method: 'POST'
            })
            try{
                uni.hideLoading()
            } catch{}
            return this.respInject(res)
        } catch{
            uni.hideLoading()
        }
        
        
    }

    async get (url:any, data?:any, showLoading:boolean=true, header?:any, isNotUserId?: boolean) {
        let params = this.getParams(url, data, header, isNotUserId)
        if (showLoading) {
            uni.showLoading()
        }
        try{
            let res = await this.fetch({
                ...params,
                method: 'GET'
            })
            try{
                uni.hideLoading()
            } catch{}
            return this.respInject(res)
        } catch{
            uni.hideLoading()
        }
    }

    getParams(url:any, data?:any, header?:any, isNotUserId?: boolean) {
        let params = {}
        const userId = uni.getStorageSync('Login-Userid')
        const tenantid = uni.getStorageSync('Tenantid')
        if (wx) {
            params = Object.assign({}, this.defaultConfig, {
                path: url,
                dataType: 'json',
                responseType: 'json',
                data,
                header: {
                    ...this.defaultConfig.header,
                    ...header,
                    'Login-Userid': isNotUserId ? null : userId,
                    Tenantid: isNotUserId ? null : tenantid
                }
            })
        } else {
            params = Object.assign({}, this.defaultConfig, {
                url: url,
                dataType: 'json',
                responseType: 'json',
                data,
                header: {
                    ...this.defaultConfig.header,
                    ...header,
                    'Login-Userid': isNotUserId ? null : userId,
                    Tenantid: isNotUserId ? null : tenantid
                }
            })
        }
        return params
    }

    respInject(res:any) {
        console.log(res)
        if (res.statusCode === 200) {
            if (res.data.code === 200) {
                return res.data.data
            } else {
                uni.showToast({
                    icon: 'error',
                    title: res.data.msg
                })
                return Promise.reject(res.data)
            }
        } else {
            uni.showToast({
                icon: 'error',
                title: res.data.msg
            })
            return Promise.reject(res.data)
        }
    }
}

export const http = Http.getInstance()